<?php


namespace app\api\model;


class Theme extends BaseModel
{
    protected $hidden = ['delete_time', 'update_time','topic_img_id', 'head_img_id'];

    public function topicImg()
    {
        // Image 的主键 ID 属于 Theme 的 外键 Topic_img_id
        return $this->belongsTo('Image', 'topic_img_id', 'id');
    }

    public function headImg()
    {
        return $this->belongsTo('Image', 'head_img_id', 'id');
    }

    public function products()
    {
        return $this->belongsToMany(Product::class, 'theme_product', 'product_id', 'theme_id');
    }

    public static function getThemeWithProducts($id)
    {
        $theme = self::with(['Products','topicImg','headImg'])->findOrEmpty($id);
        return $theme;
    }
}